package com.business.api.official.toursism.websitegoods.controller;

import com.business.api.official.toursism.websitegoods.dto.WebsiteGoodsApiDTO;
import com.business.api.official.toursism.websitegoods.from.WebsiteGoodsApiSaveFrom;
import com.business.api.official.toursism.websitegoods.from.WebsiteGoodsApiUpdateFrom;
import com.business.api.official.toursism.websitegoods.manager.WebsiteGoodsApiManager;
import com.business.api.official.toursism.websitegoods.query.WebsiteGoodsApiQuery;
import com.business.api.official.toursism.websitegoods.query.WebsiteGoodsApiPageQuery;
import com.business.api.official.toursism.websitegoods.service.WebsiteGoodsApiService;
import com.business.api.official.toursism.websitegoods.vo.WebsiteGoodsApiBriefPageVO;
import com.business.api.official.toursism.websitegoods.vo.WebsiteGoodsApiDetailPageVO;
import com.business.api.official.toursism.websitegoods.vo.WebsiteGoodsApiDetailVO;
import com.business.api.official.toursism.websitegoods.vo.WebsiteGoodsApiDetailListVO;
import com.business.api.official.toursism.websitegoods.vo.data.WebsiteGoodsApiDetailData;
import com.business.system.official.toursism.websitegoods.entity.WebsiteGoods;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.controller.SeedApiController;
import com.seed.core.pojo.dto.SeedMessageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @description 商品
 * @author CodeGenerator
 * @date   2025-01-25
 * @version V1.0
 */
@Slf4j
@RestController(value = "websiteGoodsApiController")
@Api(value = "websiteGoodsController", tags = "商品")
@RequestMapping("/api/v1/toursism/websiteGoods")
public class WebsiteGoodsApiController extends SeedApiController<WebsiteGoodsApiService, WebsiteGoodsApiManager, WebsiteGoods> {

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存")
    public WebsiteGoodsApiDetailVO save(@RequestBody @ApiParam(value = "保存表单", required = true) WebsiteGoodsApiSaveFrom websiteGoodsFrom) {
        log.info("[api-v1]WebsiteGoodsController.save,保存");

        WebsiteGoodsApiDetailVO result = new WebsiteGoodsApiDetailVO();
        if (StringUtils.isEmpty(websiteGoodsFrom)) {
            result.setCode("A0410");
            result.setMessage("表单不能为空!");
            result.setSuccess(false);
            return result;
        }
        WebsiteGoodsApiDTO apiDTO;
        try {
            apiDTO = baseService.save(websiteGoodsFrom);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteGoodsController.save ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("保存失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        WebsiteGoodsApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(apiDTO.getWebsiteGoods());
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteGoodsController.save ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("保存失败");
            return result;
        }
        result.setData(data);
        return result;
    }

    @RequestMapping(method = RequestMethod.PATCH)
    @ResponseBody
    @ApiOperation(value = "不完全更新,更新from中不为空的值（空值不会被更新）")
    public WebsiteGoodsApiDetailVO updateSelective(@RequestBody @ApiParam(value = "更新表单", required = true) WebsiteGoodsApiUpdateFrom websiteGoodsFrom) {
        log.info("[api-v1]WebsiteGoodsController.updateSelective,不完全更新,更新from中不为空的值（空值不会被更新）");

        WebsiteGoodsApiDetailVO result = new WebsiteGoodsApiDetailVO();
        if (StringUtils.isEmpty(websiteGoodsFrom)) {
            result.setCode("A0410");
            result.setMessage("更新信息不能为空!");
            result.setSuccess(false);
            return result;
        }
        if (StringUtils.isEmpty(websiteGoodsFrom.getId())) {
            result.setCode("A0410");
            result.setMessage("id信息不能为空!");
            result.setSuccess(false);
            return result;
        }
        WebsiteGoodsApiDTO apiDTO;
        try {
            apiDTO = baseService.updateSelectiveById(websiteGoodsFrom);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteGoodsController.updateSelective ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("更新失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        WebsiteGoodsApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(websiteGoodsFrom.getId());
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteGoodsController.updateSelective ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("更新失败");
            return result;
        }
        result.setData(data);
        return result;
    }

    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "删除,支持ids")
    public WebsiteGoodsApiDetailVO delete(@PathVariable @ApiParam(value = "主键id", required = true) String ids) {
        log.info("[api-v1]WebsiteGoodsController.delete,删除");

        WebsiteGoodsApiDetailVO result = new WebsiteGoodsApiDetailVO();
        if (StringUtils.isEmpty(ids)) {
            result.setCode("A0410");
            result.setMessage("id不能为空!");
            result.setSuccess(false);
            return result;
        }
        SeedMessageDTO apiDTO;
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            apiDTO = baseService.removeByIds(idList);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteGoodsController.delete ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("删除失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        return result;
    }


    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id获取商品详细信息")
    public WebsiteGoodsApiDetailVO getDetailVO(@PathVariable @ApiParam(value = "主键id", required = true) String id) {
        log.info("[api-v1]WebsiteGoodsController.getWebsiteGoodsApiVO接口,根据id获取商品详细信息");

        WebsiteGoodsApiDetailVO result = new WebsiteGoodsApiDetailVO();
        if (StringUtils.isEmpty(id)) {
            result.setCode("A0410");
            result.setMessage("id不能为空!");
            result.setSuccess(false);
            return result;
        }

        WebsiteGoodsApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(id);
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteGoodsController.getWebsiteGoodsApiVO接口 ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }
        result.setData(data);
        return result;
    }


    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部")
    public WebsiteGoodsApiDetailListVO listAll(@ApiParam(value = "查询条件", required = true) WebsiteGoodsApiQuery query) {
        log.info("[api-v1]WebsiteGoodsController.listAll,获取全部");
        WebsiteGoodsApiDetailListVO result = new WebsiteGoodsApiDetailListVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteGoodsApiQuery();
        }
        try {
            result = baseService.listAll(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteGoodsController.listAll ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }

    @RequestMapping(value = "/brief", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据查询条件查询简略对象")
    public WebsiteGoodsApiBriefPageVO getBriefPageVO(@ApiParam(value = "查询条件", required = true) WebsiteGoodsApiPageQuery query) {
        log.info("[api-v1]WebsiteGoodsController.getBriefPageVO,根据查询条件查询简略对象");

        WebsiteGoodsApiBriefPageVO result = new WebsiteGoodsApiBriefPageVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteGoodsApiPageQuery();
        }

        try {
            result = baseService.getBriefPageVO(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteGoodsController.getBriefPageVO ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据查询条件查询详细对象")
    public WebsiteGoodsApiDetailPageVO getDetailPageVO(@ApiParam(value = "查询条件", required = true) WebsiteGoodsApiPageQuery query) {
        log.info("[api-v1]WebsiteGoodsController.getDetailPageVO,根据查询条件查询详细对象");

        WebsiteGoodsApiDetailPageVO result = new WebsiteGoodsApiDetailPageVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteGoodsApiPageQuery();
        }

        try {
            result = baseService.getDetailPageVO(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteGoodsController.getDetailPageVO ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }

}